package com.fjec.common.aliyun.api;

import com.alibaba.cloudapi.sdk.constant.SdkConstant;
import com.alibaba.cloudapi.sdk.enums.HttpMethod;
import com.alibaba.cloudapi.sdk.model.ApiResponse;
import com.alibaba.cloudapi.sdk.model.HttpClientBuilderParams;
import com.fjec.common.aliyun.api.view.IdCardVO;
import com.fjec.common.util.FileUtil;
import com.fjec.common.util.JsonUtil;

import java.util.HashMap;
import java.util.Map;

public class IdCardClient extends BaseAliyunApi {

    private final static String HOST = "dm-51.data.aliyun.com";

    private final static String PATH = "/rest/160601/ocr/ocr_idcard.json";

    public IdCardClient(HttpClientBuilderParams param) {
        super(param);
    }

    @Override
    public String getHost() {
        return HOST;
    }

    @Override
    public String getPath() {
        return PATH;
    }

    @Override
    public HttpMethod getHttpMethod() {
        return HttpMethod.POST_BODY;
    }

    public static void main(String[] args) throws Exception {
        HttpClientBuilderParams param = new HttpClientBuilderParams();
        param.setAppKey(BaseAliyunApi.TEST_ACCESS_KEY_ID);
        param.setAppSecret(BaseAliyunApi.TEST_SECRET);
        IdCardClient client = new IdCardClient(param);
        String str64 = FileUtil.getFileBase64("d:\\back.jpg");
        Map<String, Object> body = new HashMap<>();
        body.put("image", str64);
        body.put("configure", "{\"side\":\"back\"}");
        String jsonStr = JsonUtil.toJson(body);
//        System.out.println(jsonStr);
        ApiResponse response = client.syncReq(jsonStr.getBytes(SdkConstant.CLOUDAPI_ENCODING));
        String resultStr = new String(response.getBody() , SdkConstant.CLOUDAPI_ENCODING);
        Map<String, Object> map = JsonUtil.jsonToMap(resultStr);
        IdCardVO vo = IdCardVO.build(map);
        System.out.println("getNum=" + vo.getNum());
        System.out.println("getName=" + vo.getName());
        System.out.println("getSex=" + vo.getSex());
        System.out.println("getBirth=" + vo.getBirth());
        System.out.println("getAddress=" + vo.getAddress());
        System.out.println("getNationality=" + vo.getNationality());
        System.out.println("getStartDate=" + vo.getStartDate());
        System.out.println("getEndDate=" + vo.getEndDate());
        System.out.println("getIssue=" + vo.getIssue());
        System.out.println("isFake=" + vo.isFake());
    }
}
